package com.springinaction.ch06.data;

import com.springinaction.ch06.Spittle;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Component
public class SpittleRepositorylmpl implements SpittleRepository {
    private static  List<Spittle> spittleList = createRepository();
    @Override
    public List<Spittle> findSpittles(long max, int count) {

        return spittleList.subList(0, Math.max(Math.min(Math.min((int) max, count - 1), 25), 0));
    }

    @Override
    public Spittle findOne(int spittleId) {
        return spittleList.get(Math.max(0, Math.min(24, spittleId)));
    }

    private static List<Spittle> createRepository() {
        List<Spittle> list = new ArrayList<Spittle>(20);
        for (int i = 0; i < 25; i++) {
            list.add(new Spittle(Long.valueOf(i), Integer.toString(i), new Date(), Math.random(), Math.random()));
        }
        return list;
    }
}
